package com.zjuee.web.company.controller;

import com.zjuee.base.bean.response.R;
import com.zjuee.base.constant.Common;
import com.zjuee.service.company.service.CompanyLicenseClassService;
import com.zjuee.service.company.service.CompanyLicenseGroupService;
import com.zjuee.service.company.service.CompanyLicenseService;
import com.zjuee.service.company.vo.CompanyLicenseVo;
import com.zjuee.service.company.vo.CompanyOtherProVo;
import com.zjuee.base.model.common.company.CompanyLicense;
import com.zjuee.base.model.common.company.CompanyLicenseClass;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;

@Slf4j
@RestController
@RequestMapping(Common.Service.ZJUEE_CO_AUTH_COMPANY_LICENSE_URL_PREFIX)
public class CompanyLicenseController{

    @Resource
    private CompanyLicenseService licenseService;

    @Resource
    private CompanyLicenseClassService licenseClassService;

    @Resource
    private CompanyLicenseGroupService licenseGroupService;

    /**
     * 根据企业Id查询详情 - 整体
     *
     * @param companyId Long
     * @return CompanyVo
     */
    @GetMapping("/info/company/{id}")
    public R<List<CompanyLicenseVo>> selectByCompanyId(@NotNull @PathVariable(value = "id") Long companyId) {
        try {
            List<CompanyLicenseVo> select = licenseService.selectByCompanyId(companyId);
            if (null != select) {
                return R.ok(select);
            }
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.fail("Resource does not exist");
    }

    /**
     * 根据条件查询许可证信息
     *
     * @param companyLicense CompanyLicense
     * @return CompanyVo
     */
    @PostMapping("/list/all")
    public R<List<CompanyLicenseVo>> list(@RequestBody CompanyLicense companyLicense) {
        try {
            List<CompanyLicenseVo> select = licenseService.listAll(companyLicense);
            if (null != select) {
                return R.ok(select);
            }
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.fail("Resource does not exist");
    }

    /**
     * 根据许可证号查询外省企业信息
     *
     * @param jqxkzbh String
     * @return CompanyOtherProVo
     */
    @GetMapping("/info/company/other/pro")
    public R<CompanyOtherProVo> getCompanyOtherProInfo(@RequestParam(value = "jqxkzbh") String jqxkzbh) {
        try {
            CompanyOtherProVo select = licenseService.getCompanyOtherProInfo(jqxkzbh);
            if (null != select) {
                return R.ok(select);
            }
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.fail("Resource does not exist");
    }

    /**
     * 根据企业id查询许可证大类信息
     *
     * @param companyId Long
     * @return List<CompanyLicenseClass>
     */
    @GetMapping("/all/b/class")
    public R<List<CompanyLicenseClass>> getAllBClass(@RequestParam(value = "companyId") Long companyId, @RequestParam(value = "licenseType") String licenseType) {
        try {
            List<CompanyLicenseClass> select = licenseClassService.getAllBClass(companyId, licenseType);
            if (null != select) {
                return R.ok(select);
            }
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.fail("Resource does not exist");
    }

    /**
     * 根据许可证，大类代码 查询小类
     *
     * @param licenseId Long
     * @param bCode String
     * @return List<CompanyLicenseVo>
     */
    @GetMapping("/all/s/class")
    public R<List<CompanyLicenseClass>> getAllSClass(@RequestParam(value = "licenseId")Long licenseId,  @RequestParam(value = "bCode")String bCode) {
        try {
            List<CompanyLicenseClass> select = licenseClassService.getAllSClass(licenseId, bCode);
            if (null != select) {
                return R.ok(select);
            }
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.fail("Resource does not exist");
    }

    //根据代码小类查询处置大类
    @GetMapping("/all/b/disposal")
    public R<List<CompanyLicenseVo>> getAllBDisposal(@RequestParam(value = "licenseId")Long licenseId,@RequestParam(value = "sCode") String sCode) {
        try {
            List<CompanyLicenseVo> select = licenseClassService.getAllBDisposal(licenseId, sCode);
            if (null != select) {
                return R.ok(select);
            }
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.fail("Resource does not exist");
    }

    //根据处置大类查询小类
    @GetMapping("/all/s/disposal")
    public R<List<CompanyLicenseVo>> getAllSDisposal(@RequestParam(value = "licenseId")Long licenseId, @RequestParam(value = "disposalType") String disposalType) {
        try {
            List<CompanyLicenseVo> select = licenseGroupService.getAllSDisposal(licenseId, disposalType);
            if (null != select) {
                return R.ok(select);
            }
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.fail("Resource does not exist");
    }

    //根据代码大类、代码小类查询许可证信息
    @GetMapping("/all/license")
    public R<List<CompanyLicenseVo>> getAllLicense(@RequestParam(value = "bCode")String bCode,@RequestParam(value = "sCode") String sCode) {
        try {
            List<CompanyLicenseVo> select = licenseClassService.getAllLicense(bCode, sCode);
            if (null != select) {
                return R.ok(select);
            }
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.fail("Resource does not exist");
    }
}
